package hash;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description: 454.四数相加
 * User: Li_yizYa
 * Date: 2025—04—02
 * Time: 18:47
 */
public class Solution5 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2};
        int[] nums2 = {-2, -1};
        int[] nums3 = {-1, 2};
        int[] nums4 = {0, 2};
        System.out.println(fourSumCount(nums1, nums2, nums3, nums4));
    }

    private static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int ret = 0;
        Map<Integer, Integer> map = new HashMap<>();
        //先统计前两个数组元素和，并将其放入map中
        for (int i : nums1) {
            for (int j : nums2) {
                int sum = i + j;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }
        //统计后两个数组元素和
        for (int i : nums3) {
            for (int j : nums4) {
                int sum = 0 - i - j;
                if (map.containsKey(sum)) {
                    ret += map.get(sum);
                }
            }
        }
        return ret;
    }
}
